﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OnlineCourse.Services;
using OnlineCourse.Model;
using OnlineCourse.Services.Model;
namespace OnlineCourse.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BuyOrderController : ControllerBase
    {
        private readonly BuyOrderService _buyOrderService;
        public BuyOrderController(ApplicationDbContext db)
        {
            this._buyOrderService = new BuyOrderService(db);
        }
        /// <summary>
        /// 接口，购买一个新的课程，不用券
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="courseid"></param>
        /// <returns></returns>
        [HttpPost("Add")]
        public async Task<IActionResult> BuyOrderAdd(Guid userid, Guid courseid)
        {
            bool mark = false;
            int mes=1000;
            
            await Task.Run(() => { mark = _buyOrderService.BuyOrderAdd(out mes, userid, courseid);});
            return Ok(new { mes = mes, mark = mark });
        }
        /// <summary>
        /// 购买一个新的课程，用券
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="courseid"></param>
        /// <param name="couponid"></param>
        /// <returns></returns>
        [HttpPost("AddByCoupon")]
        public async Task<IActionResult> BuyOrderAdd(Guid userid, Guid courseid, Guid couponid)
        {
            bool mark = false;
            int mes = 1000;
            
            await Task.Run(
                () =>
                {
                    mark = _buyOrderService.BuyOrderAdd(out mes,userid, courseid, couponid);
                }
                );
            return Ok(new { mes=mes,mark=mark});
        }
        /// <summary>
        /// 获取一个订单的详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("GetOrder")]
        public async Task<IActionResult> getBuyOrder(Guid id)
        {
            BuyOrderQueryModel bwdq = new BuyOrderQueryModel();
            await Task.Run(() => { bwdq = _buyOrderService.getBuyOrder(id); });
            return Ok(bwdq);
        }
        /// <summary>
        /// 获取一个无条修的分页的订单列表
        /// </summary>
        /// <param name="total"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpPost("GetList")]
        public async Task<IActionResult> getBuyOrderQueryList(int total, int page = 1, int pageSize = 10)
        {
            List<BuyOrderQueryModel> list = new List<BuyOrderQueryModel>();

            await Task.Run(() => { list = _buyOrderService.getBuyOrderQueryList(out total, page = 1, pageSize = 10); });
            return Ok(new { total=total,data= list });
        }
        /// <summary>
        /// 获取一个订单清单，按用户查询
        /// </summary>
        /// <param name="id"></param>
        /// <param name="total"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpPost("GetListByUserid")]
        public async Task<IActionResult> getBuyOrderQueryList(Guid id, int total, int page = 1, int pageSize = 10)
        {
            List<BuyOrderQueryModel> list = new List<BuyOrderQueryModel>();
            await Task.Run(() => { list = _buyOrderService.getBuyOrderQueryList(id, out total, page = 1, pageSize = 10); });
            return Ok(list);
        }
        /// <summary>
        /// 获取一个按时间条件以标题和类型查询的订单
        /// </summary>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns></returns>
        [HttpPost("GetGroupList")]
        public async Task<IActionResult> GetGroupByTimeBuyOrders(DateTime date1, DateTime date2)
        {
            if (date2 >= date1)
            {
                List<GroupByTimeBuyOrder> list = new List<GroupByTimeBuyOrder>();
                await Task.Run(() => { list = _buyOrderService.GetGroupByTimeBuyOrders(date1, date2); });
                return Ok(list);
            }
            else
            {
                return Ok(false);
            }
        }
    }
}